
package com.shop.cloud.mall.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.mall.common.config.MallConfigProperties;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.entity.ShopApply;
import com.shop.cloud.mall.admin.service.ShopApplyService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 店铺入驻申请表
 *
 * @author
 * @date 2020-10-28 10:39:58
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/shopapply")
@Tag(description = "shopapply", name = "店铺入驻申请表管理")
public class ShopApplyController {

    private final ShopApplyService shopApplyService;
	private final MallConfigProperties mallConfigProperties;
	private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();
	private final RedisTemplate redisTemplate;

    /**
     * 分页列表
     * @param page 分页对象
     * @param shopApply 店铺入驻申请表
     * @return
     */
    @Operation(summary = "分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:shopapply:index')")
    public R getPage(Page page, ShopApply shopApply) {
        return R.ok(shopApplyService.page(page, Wrappers.query(shopApply)));
    }

    /**
     * 店铺入驻申请表查询
     * @param id
     * @return R
     */
    @Operation(summary = "店铺入驻申请表查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:shopapply:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(shopApplyService.getById(id));
    }

    /**
     * 店铺入驻申请表修改
     * @param shopApply 店铺入驻申请表
     * @return R
     */
    @Operation(summary = "店铺入驻申请表修改")
    @PutMapping("/addedit")
    public R updateById(@RequestBody ShopApply shopApply) {
		if(StrUtil.isNotBlank(shopApply.getId())){
			ShopApply shopApply2 = shopApplyService.getById(shopApply.getId());
			if("1".equals(shopApply2.getStatus())){
				return R.failed("该状态不支持修改");
			}
			shopApplyService.updateById(shopApply);
		}else{
			//验证码合法校验
			String key = CacheConstants.VER_CODE_DEFAULT + CommonConstants.PHONE_CODE_5 + ":" + shopApply.getUserPhone();
			Object codeObj = redisTemplate.opsForValue().get(key);
			if (codeObj == null) {
				return R.failed("验证码不合法");
			}
			String saveCode = codeObj.toString();
			if (StrUtil.isBlank(saveCode) || !StrUtil.equals(saveCode, shopApply.getSmsCode())) {
				redisTemplate.delete(key);
				return R.failed("验证码不合法");
			}

			shopApply.setStatus(MallConstants.SHOPAPPLY_STATUS_0);
			shopApply.setImgUrl(mallConfigProperties.getUserDefaultAvatar());
			shopApply.setUserAvatar(mallConfigProperties.getUserDefaultAvatar());
			TenantContextHolder.setTenantId(shopApply.getTenantId());
			shopApply.setUserPassword(shopApply.getUserPassword());
			shopApply.setTenantId(null);
			shopApplyService.save(shopApply);
			TenantContextHolder.clear();
		}
		return R.ok(shopApply);
    }

    /**
     * 店铺入驻申请表删除
     * @param id
     * @return R
     */
    @Operation(summary = "店铺入驻申请表删除")
    @SysLog("删除店铺入驻申请表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:shopapply:del')")
    public R removeById(@PathVariable String id) {
        return R.ok(shopApplyService.removeById(id));
    }

	/**
	 * 店铺入驻申请审核
	 * @param shopApply 店铺入驻申请表
	 * @return R
	 */
	@Operation(summary = "店铺入驻申请审核")
	@PutMapping("/verify")
	@PreAuthorize("@ato.hasAuthority('mall:shopapply:verify')")
	public R verify(@RequestBody ShopApply shopApply) {
		try{
			shopApplyService.verify(shopApply);
		}catch (Exception e){
			if(e.getMessage().contains("uk_phone")){
				return R.failed("该店长手机号已存在");
			}if(e.getMessage().contains("uk_username")){
				return R.failed("该店长账号已存在");
			}if(e.getMessage().contains("uk_email")){
				return R.failed("该店长邮箱已存在");
			}else{
				return R.failed(e.getMessage());
			}
		}
		return R.ok();
	}

	/**
	 * 查询
	 * @param shopApply
	 * @return
	 */
	@Operation(summary = "查询")
	@GetMapping("/one")
	public R get(ShopApply shopApply) {
		TenantContextHolder.setTenantId(shopApply.getTenantId());
		shopApply = shopApplyService.getById(shopApply.getId());
		return R.ok(shopApply);
	}
}
